﻿using System;
using System.Linq;
using System.Windows.Forms;
using Coffee_Restaurant.Database;

namespace Coffee_Restaurant.Forms.AreaTable
{
    public partial class AreaAddEdit : Form
    {
        //event edit
        public delegate void ButtonSaveEventHandler(string key, string name);
        public event ButtonSaveEventHandler BtnAddArea;

        //event save
        public delegate void ButtonSaveEditEventHandler(string key, string name);
        public event ButtonSaveEditEventHandler BtnEditArea;

        //idEdit
        public int AreaId { get; set; }

        public AreaAddEdit()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        public bool ValidateForm()
        {
            if (string.IsNullOrEmpty(txtNameArea.Text) || string.IsNullOrWhiteSpace(txtNameArea.Text))
            {
                txtNameArea.BackColor = System.Drawing.Color.Khaki;
                txtNameArea.Focus();
                return false;
            }
            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (btnSave.Name == "btnSaveEdit")
            {
                if (BtnEditArea != null && ValidateForm() && AreaId > 0)
                {
                    try
                    {
                        using (var db = new DatabaseEntities())
                        {
                            //save to database
                            var model = db.Areas.FirstOrDefault(a => a.AreaId == AreaId);
                            if (model != null)
                            {
                                model.Name = txtNameArea.Text;
                                model.Description = txtDescription.Text;
                            }
                            if (db.SaveChanges() > 0)
                            {
                                if (model != null) BtnEditArea(model.AreaId.ToString(), model.Name);
                                this.Hide();
                            }
                        }
                    }
                    catch (Exception)
                    {
                        BtnEditArea(null, null);
                    }
                }
            }
            else
            {
                if (BtnAddArea != null && ValidateForm())
                {
                    try
                    {
                        using (var db = new DatabaseEntities())
                        {
                            //add to database
                            var guid = Guid.NewGuid().ToString();
                            db.Areas.Add(new Area() { Name = txtNameArea.Text, Icon = "", Description = txtDescription.Text, Date = DateTime.Now, GuiId = guid });
                            if (db.SaveChanges() > 0)
                            {
                                var model = db.Areas.FirstOrDefault(a => a.GuiId == guid);
                                if (model != null)
                                {
                                    BtnAddArea(model.AreaId.ToString(), model.Name);
                                }
                                this.Hide();
                            }
                        }
                    }
                    catch (Exception)
                    {
                        BtnAddArea(null, null);
                    }
                }
            }
        }


    }
}
